<template>
  <view class="project_info">
    <view v-for="(item,index) in listInfo" :key="index" class="project_info_item">
      <view class="project_info_item_label">{{ item.label }}:</view>
      <u-input v-model="item.value" class="project_info_item_value" :disabled="disabled" />
    </view>
    <view v-if="!disabled" class="addBtn">
      <button v-if="!fileObj.file" type="primary" @click="addFile">上传附件</button>
      <button v-else type="primary" @click="addFile">修改附件</button>
    </view>
    <view v-if="!disabled" class="sureBtn">
      <button type="primary" @click="sureBtn">确定</button>
    </view>
  </view>
</template>

<script>
import { queryHeaderByMenuId, queryHeaderByTableId, uploadFileMulti } from '@/api/formview';
import { Message, getCurrentTime } from '@/utils/utils';
import { chooseFile } from '@/utils/files.js';
import { saveDynamic } from '@/api/file';
export default {
  name: 'ProjectInfo',
  data() {
    return {
      listInfo: [],
      disabled: false,
      FILEID: '',
      menuId: undefined,
      tableId: undefined,
      headData: [],
      fileObj: {},
      parentHeaders: []
    };
  },
  created() {
    this.getRouteParams();
  },
  methods: {
    async getRouteParams() {
      const { path } = this.$route;
      if (path === '/uploadFile') {
        this.disabled = false;
        const { FILEID, menuId } = this.$route.query;
        this.FILEID = FILEID;
        this.menuId = menuId;
        this.listInfo = [];
        await this.getTableId();
        this.tableId && this.getTableHead();
      } else {
        this.disabled = true;
        const { listInfo, FILEID } = this.$route.query;
        this.listInfo = JSON.parse(listInfo);
        this.FILEID = FILEID;
      }
    },
    async getTableId() {
      const { data, success, message } = await queryHeaderByMenuId({ menuId: this.menuId });
      if (success) {
        this.parentHeaders = data.parentHeaders;
        const childrenTableInfos = data.childrenTableInfos.filter(item => item.display).sort((a, b) => a.orderId - b.orderId)[0];
        childrenTableInfos && (this.tableId = childrenTableInfos.id);
      } else {
        this.tableId = '';
        Message(message);
      }
    },
    async getTableHead() {
      const { data, success, message } = await queryHeaderByTableId({ tableId: this.tableId });
      if (success) {
        const filterData = data.filter(i => i.isMobileDisplay);
        filterData.sort((a, b) => a.orderId - b.orderId);
        this.listInfo = [];
        filterData.forEach(item => {
          this.listInfo.push(
            {
              label: item.title,
              value: '',
              dataIndex: item.dataIndex
            }
          );
        });
        this.headData = filterData;
      } else {
        this.listInfo = [];
        this.headData = [];
        Message(message);
      }
    },
    async addFile() {
      const { files } = await chooseFile();
      const { CENTERPRISECODE1, CPROGRAMNAME, ENTERPRISENAME } = this.$route.query;
      this.fileObj = {
        FILENAME: files[0].name,
        FILETYPE: files[0].type,
        // eslint-disable-next-line no-undef
        UPLOADER: uni.getStorageSync('userName'),
        UPLOADDATE: getCurrentTime(),
        file: files[0],
        YEAR: getCurrentTime().split('-')[0],
        CENTERPRISECODE1,
        CPROGRAMNAME,
        CENTERPRISENAME1: ENTERPRISENAME
      };
      this.listInfo.forEach(v => {
        if (this.fileObj[v.dataIndex]) {
          v.value = this.fileObj[v.dataIndex];
        }
      });
    },
    async sureBtn() {
      if (!this.fileObj.file) {
        return Message('请先上传附件');
      }
      const obj = {
        menuId: this.menuId,
        bodyData: [
          {
            data: [
              { option: 'create' }
            ],
            tableId: this.tableId + ''
          },
          {
            data: [
              {
                option: 'create',
                ACCEPTANCEFORMNUM: '提交时自动生成'
              }
            ],
            tableId: this.parentHeaders[0].tableId
          }
        ]
      };
      this.listInfo.forEach(v => {
        obj.bodyData[0].data[0][v.dataIndex] = v.value;
      });
      const { data: { formGuid }, success, message } = await saveDynamic(obj);
      if (success) {
        const requestData = new FormData();
        requestData.append('files', this.fileObj.file);
        requestData.append('guid', formGuid);
        requestData.append('menuId', JSON.stringify(this.menuId));
        requestData.append('description', this.tableId + '');
        const res = await uploadFileMulti(requestData);
        if (res.success) {
          Message(res.message);
          setTimeout(() => {
            this.$router.go(-1);
          }, 1000);
        }
      } else {
        Message(message);
      }
    }
  }
};
</script>

<style lang="less" scoped>
.project_info{
  background-color: #F5F7FA;
  padding-top: 12px;
  padding-bottom: 50px;
  .project_info_item{
    height: 50px;
    line-height: 50px;
    background-color: #fff;
    border-bottom: 1px solid #dee1e6;
    display: flex;
    font-size: 17px;
    .project_info_item_label{
      padding-left: 12px;
      color: #303133;
    }
    .project_info_item_value{
      padding-left: 8px !important;
      color: #737373;
      height: 100%;
      /deep/.u-input__input{
        height: 100%;
        font-size: 17px;
      }
    }
  }
  .addBtn {
    padding-top: 10px;
  }
  .sureBtn{
    position: fixed;
    bottom: 0;
    width: 100%;
  }
}
</style>
